package com.typing.alarm.receiver;

import java.util.List; 

import com.typing.alarm.R;
import com.typing.alarm.activity.AlarmRingingActivity;
import com.typing.alarm.activity.TypingAlarmActivity;
import com.typing.alarm.value.TypingAlarmVO;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.widget.Toast;

public class TypingAlarmReceiver extends BroadcastReceiver{
	private TypingAlarmVO alarmInfo;
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Log.d("TypingAlarmReceiver","Receive start");
		alarmInfo = (TypingAlarmVO)intent.getParcelableExtra("alarm_info");		
		showNotification(context,R.drawable.ic_launcher,"알람!!","지금이러고 있을 시간이 없다.");
		
		Log.d("TypingAlarmReceiver","Receive Complete");
	}
	private void showNotification(Context context,int statusBarIconId,String statusBarTextId,String detailedTextId){
		Intent contentIntent = new Intent(context,AlarmRingingActivity.class);
		contentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		contentIntent.putExtra("alarm_info", alarmInfo);
		PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		CharSequence from = "Typing Alarm";
		CharSequence message = "("+alarmInfo.getTypingText() + ") 문장을  " + alarmInfo.getTypingSpeed() +"타 이상으로 치셔야 알람이 중지됩니다.";
		
		Notification notice = new Notification(statusBarIconId,null,System.currentTimeMillis());
		//notice.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
		notice.flags = Notification.FLAG_INSISTENT;
		notice.setLatestEventInfo(context, from, message, appIntent);
		notice.ledARGB = Color.GREEN;		
		NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		nm.notify(1234,notice);
		
		ActivityManager mgr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> info = mgr.getRunningTasks(1);
		ComponentName topActivity = info.get(0).topActivity;
		String name = topActivity.getClassName();
		
		if(!AlarmRingingActivity.class.toString().endsWith(name)){		
			//Intent intent = new Intent(context,AlarmRingingActivity.class);
			//contentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);	
			//intent.putExtra("alarm_info", alarmInfo);
			//context.startActivity(contentIntent);
			contentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			try{
				appIntent.send();
			}catch(CanceledException e){
				e.printStackTrace();
			}
		}
	}

}
